import {AbilityModel} from "../../controller/model/AbilityModel";
import Configer from "../../frame/apiModel/config/Configer";
import {FinalKey} from "../../constant/FinalConstant";
import {UnitController} from "../../controller/UnitController";
import {Logger} from "../../frame/apiModel/config/Logger";
import {UnitModel} from "../../frame/apiModel/adapterModel/model3/UnitModel";
import {ItemModel} from "../../frame/apiModel/adapterModel/model3/ItemModel";
import {PlayerModel} from "../../frame/apiModel/adapterModel/model4/PlayerModel";
import {LocModel} from "../../frame/apiModel/base/LocModel";
import {PlayerFactory} from "../../frame/PlayerFactory";
import {DataRouterUtil} from "../../utils/router/DataRouterUtil";
import {CodeUtil} from "../../frame/codeAdapter/CodeUtil";
import {AttributeEnum} from "../../frame/dataSource/DataModel";
import {Util} from "../../utils/Util";
import {AbilityUtil} from "./AbilityUtil";
import UnitUtil from "../../utils/UnitUtil";
import {DataRouter} from "../../frame/dataSource/DataRouter";
import {ModelFactory} from "../../frame/apiModel/adapterModel/model5/ModelFactory";
import {ConfigKey} from "../../constant/ConfigConstant";
import {GoodController} from "../../controller/GoodController";
import {ItemJson} from "../../frame/apiModel/model/StoreJson";

export default class ResetGoodSkillModel extends AbilityModel {
    abilityId: string = Configer.getFinal(FinalKey.resetGoodSkill) as string;

    actionFun = (activeUnit: UnitModel, activeItem: ItemModel, targetUnit: UnitModel, targetItem: ItemModel, activePlayer: PlayerModel, targetPlayer: PlayerModel, spellId: string, spellLoc: LocModel)  => {
        let success;
        this.resetGoodSkill(activeUnit, activePlayer);
        return success;
    }

    resetGoodSkill (activeUnit: UnitModel, player: PlayerModel) {
        const item = activeUnit.getFirstItem()
        if (item) {
        } else {
            Logger.toastProduct(`单位第一栏没有物品`, player)
            return;
        }
        const goodId = item.strId;
        const goodData = DataRouter.getItemDataByItemId(goodId);
        if (goodData) {
        } else {
            Logger.toastProduct(`只有装备才能重铸`, player)
            return;
        }
        let quality = goodData.quality;
        // 重铸装备成功几率20  失败几率5
        const upQualityPro: number = Configer.getConfig(ConfigKey.resetGoodSuccess);
        const downQualityPro: number = Configer.getConfig(ConfigKey.resetGoodFailed);
        let str = '当前品质装备无法重铸';
        const random = player.getRandomInt(0, 100);
        if (random < downQualityPro) {
            quality = quality - 1;
            Logger.toastProduct(`品质降低！`, player)
            str = `当前品质已经最低，重铸失败`;
        } else if (random <= (downQualityPro + upQualityPro)) {
            quality = quality + 1;
            Logger.toastProduct(`品质提升！`, player)
            str = `没有更高品质的装备了, 重铸失败`;
        } else {
            Logger.toastProduct(`品质不变！`, player)
        }
        const goodIds = DataRouter.getGoodIdsByQuality(quality);
        if (goodIds && goodIds.length && goodIds.length > 0) {
        } else {
            Logger.toastProduct(str, player)
            return;
        }
        const len = goodIds.length - 1;
        const goodRandom = player.getRandomInt(0, len);
        const newGoodId = goodIds[goodRandom];

        GoodController.getInstance().removeGoods(activeUnit, item);
        const itemJson: ItemJson = { id: newGoodId, n: 0, cd: 0, lv: 0 };
        ModelFactory.getInstance().createItemInUnit(itemJson, activeUnit, undefined, true)
    }
}